package cn.bs.seckill.controller;

import cn.bs.seckill.pojo.User;
import cn.bs.seckill.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

//跳转商品页面
@Controller
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private UserService userService;
    @RequestMapping("/toList")
    //通过session获取用户的登录信息
    //通过Model显示用户的信息
    //@CookieValue拿到cookie的值
    public String toList(HttpSession session, Model model, @CookieValue("userUUID")String uuid) {
        if (StringUtils.isEmpty(uuid)){
            return "login";
        }
        //拿到用户的登录信息
//        User attribute = (User)session.getAttribute(uuid);
        User attribute = userService.getUserByCookie(uuid);
        if(null==attribute){
            return "login";
        }
        //让前端显示用户的信息
        model.addAttribute("user",attribute);
        return "goodsList";
    }
}